﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using System.Xml.Linq;
using System.Reflection.Metadata;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using Castle.Core.Resource;
using MailKit.Search;

namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    ///  组件CRM_发货申请送货方式校验
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GZCRMShipTypeCheckController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con_zh;
        public readonly string con_qh;
        public readonly string con_yb;
        public readonly string con_oa;


        public GZCRMShipTypeCheckController(ILogger<GZCRMShipTypeCheckRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            con_zh = _configuration.GetConnectionString("WMSDbConnection_ZH");
            con_qh = _configuration.GetConnectionString("WMSDbConnection");
            con_yb = _configuration.GetConnectionString("WMSDbConnection_YB");
            con_oa = _configuration.GetConnectionString("OA_DbConnection");
            _logger = loggerFactory.CreateLogger<GZCRMShipTypeCheckController>();
        }

        /// <summary>
        /// 组件CRM_发货申请送货方式校验
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostShipTypeCheck([FromBody] GZCRMShipTypeCheckRequestDto strparm)
        {
            string strMsg = "";
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
            GZCRMShipTypeCheckResponseDto re = new GZCRMShipTypeCheckResponseDto();

            try
            {
                dic = new Dictionary<string, string>();
                dic.Add("c_htbh", strparm.customer_po);
                string sqlstr = MethodHelp.GetTranSQL("TMS0149", dic, con);//查询OA合同运输方式数据
                if (sqlstr!="")
                {
                    DataTable dt_bmx = DB.MSSQuery(sqlstr, con_oa);
                    if (dt_bmx.Rows.Count>0)
                    {
                        if (dt_bmx.Rows[0]["ysfcd"].ToString()=="0")
                        {
                            if (!strparm.shiptype_name.Contains("自提"))
                            {
                                strMsg = "合同号=" + strparm.customer_po + ",物流配送方式为<客户自提且全额承担>,不能选择<"+ strparm.shiptype_name + ">类型配送！";
                            }
                        }

                        if (dt_bmx.Rows[0]["ysfcd"].ToString() != "0"&& dt_bmx.Rows[0]["ysfcd"].ToString()!="")
                        {
                            if (strparm.shiptype_name.Contains("自提"))
                            {
                                strMsg = "合同号=" + strparm.customer_po + ",物流配送方式=<"+ dt_bmx.Rows[0]["selectname"].ToString() + ">,不能选择<客户自提>进行发货！";
                            }
                        }
                    }
                    else
                    {
                        strMsg = "合同号="+strparm.customer_po+"获取OA数据失败，请确认后再试！";
                    }


                }
                else
                {
                    strMsg = "TMS0149  转化SQL语句失败，请联系管理员！";
                }

            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

            if (strMsg == "")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;

            }


            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);

        }


    }
}
